package com.sinoglobal.ningxia.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.EnentItemActivity;
import com.sinoglobal.ningxia.activity.amusement.BusinessDetailActivity;
import com.sinoglobal.ningxia.activity.amusement.PhotoBigActivity;
import com.sinoglobal.ningxia.beans.BusinessDetailVo;
import com.sinoglobal.ningxia.beans.CommentInfoVo;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.http.ConnectionUtil;
import com.sinoglobal.ningxia.utils.EquipmentUtil;
import com.sinoglobal.ningxia.utils.MContants;
import com.sinoglobal.ningxia.utils.TextUtil;
import com.sinoglobal.ningxia.widget.FiveStarsSeekBar;
import com.sinoglobal.ningxia.widget.HorizontialListView;

import net.tsz.afinal.FinalBitmap;

import java.util.ArrayList;

public class CommentAdapter extends BaseAdapter implements ListAdapter,
		OnItemClickListener {
	private TextView businessTime, businessPhone, businessAddress, actionDisc;// 营业时间,联系方式,地址,活动描述
	private ImageView businessAd, actionPhoto;// 详情广告图,活动图片
	private LinearLayout businessAction; // 活动
	private LinearLayout fiveStarLayout;// 星级评定布局
	private FiveStarsSeekBar fiveStar;// 五星等级
	private FinalBitmap afinal;// 图片显示帮助类
	private HorizontialListView hListView;// 商家照片
	private ArrayList<String> photoList;// 照片列表
	private BusinessDetailActivity context;
	private ArrayList<CommentInfoVo> commentList = null;// 评论列表
	private LayoutInflater layoutInflater;// 布局解析器
	private BusinessDetailVo businessDetail;// 商家详情
	private LinearLayout businessInfoDianping;// 点评栏）（要求没有评论的时候也要显示）
	private LinearLayout businessPhotoes;// 图片栏）（要求没有评论的时候也要显示）
	private LayoutParams lpLv;
	private LayoutParams lpLvItem;
	private FinalBitmap finalBitmap43;// 宽高比为4：3

	public CommentAdapter(BusinessDetailActivity context,
			ArrayList<CommentInfoVo> commentList) {
		this.context = context;
		this.commentList = commentList;
		afinal = FinalBitmap.create(context);
		finalBitmap43 = FinalBitmap.create(context);
		finalBitmap43.configLoadingImage(R.drawable.default43);
		finalBitmap43.configLoadfailImage(R.drawable.default43);

		afinal.configLoadfailImage(R.drawable.default43);
		afinal.configLoadingImage(R.drawable.default43);

		layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void setBusinessDetail(BusinessDetailVo result) {
		this.businessDetail = result;
		notifyDataSetChanged();
	}

	public void addData(ArrayList<CommentInfoVo> list) {
		this.commentList.clear();
		this.commentList.addAll(list);
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return commentList.size() + 1;
	}

	@Override
	public CommentInfoVo getItem(int position) {
		return commentList.get(position - 1);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// 照片墙
		// if (position == 5) {
		/*
		 * if (TextUtil.stringIsNull(FlyApplication.userId)) { Intent intent =
		 * new Intent(context, LoginActivity.class);
		 * context.startActivity(intent); // } else { // Intent intent = new
		 * Intent(context, PhotoWallActivity.class); //
		 * intent.putExtra(BusinessDetailActivity.SHOPID, //
		 * businessDetail.getShopId()); // context.startActivity(intent); // } }
		 * else { Intent intent = new Intent(context, PhotoBigActivity.class);
		 * businessDetail.setSelectedImgPosition(position);
		 * intent.putExtra(BusinessDetailVo.class.getCanonicalName(),
		 * businessDetail); context.startActivity(intent); }
		 */
		Intent intent = new Intent(context, PhotoBigActivity.class);
		businessDetail.setSelectedImgPosition(position);
		intent.putExtra(BusinessDetailVo.class.getCanonicalName(),
				businessDetail);
		context.startActivity(intent);

	}

	private void setData(final BusinessDetailVo result) {
		if (result == null) {
			return;
		}
		photoList = result.getImglist();
		String time = context.getString(R.string.business_time) + "\t" + result.getShop_open();
		// time=TextUtil.ToDBC(time);
		businessTime.setText(time);
		businessPhone.setText(context.getString(R.string.user_tel1) + result.getShop_tel());
		businessAddress.setText(context.getString(R.string.user_address1) + result.getShop_address());
		afinal.display(businessAd,
				ConnectionUtil.localUrl + result.getShop_img());
		// 根据服务器返回的星星的getShop_star值来显示当前评分
		fiveStar.setSelectNums(Integer.parseInt(result.getShop_star()));
		fiveStarLayout.addView(fiveStar);
		// int imgwidth = 0;
		int imgsize;
		try {
			imgsize = result.getImglist().size();
		} catch (Exception e) {
			// TODO: handle exception
			imgsize = 0;
		}
		//如果屏幕的分辨率大于500的时候则将图片的个数显示为4个，如果是小于500，则显示listview的全部
		// if (FlyApplication.widthPixels > 500) {
		// imgwidth = FlyApplication.widthPixels /5;
		// if (imgsize < 5) {
		// lp_lv = new LayoutParams(imgsize * imgwidth, imgwidth);
		// } else {
		// lp_lv = new LayoutParams(LayoutParams.WRAP_CONTENT, imgwidth);
		// }
		// hListView.setLayoutParams(lp_lv);
		// }
		// else {
		// imgwidth = FlyApplication.widthPixels / 3+15;
		// if (imgsize < 4) {
		// lp_lv = new LayoutParams(imgsize * imgwidth, imgwidth * 143 / 170);
		// } else {
		// lp_lv = new LayoutParams(LayoutParams.WRAP_CONTENT, imgwidth * 143 /
		// 170);
		// }
		// }

		int imgwidth = FlyApplication.widthPixels / 3 - 45;
		if (imgsize < 4) {
			lpLv = new LayoutParams(imgsize * imgwidth, imgwidth * 107 / 128);
		} else {
			lpLv = new LayoutParams(LayoutParams.WRAP_CONTENT,
					imgwidth * 107 / 128);
		}
		hListView.setLayoutParams(lpLv);
		lpLvItem = new LayoutParams(imgwidth,
				(FlyApplication.widthPixels / 3 - 45) * 107 / 128);
		if (photoList != null && !photoList.isEmpty()) {

			hListView.setAdapter(new HorizontialListViewAdapter(photoList));
			// hListView.setAdapter(new PhotoAdapter(context, photoList,
			// PhotoAdapter.TAG_BUSINESS_PHOTO));
			hListView.setOnItemClickListener(this);
			businessPhotoes.setVisibility(View.VISIBLE);
		} else {
			businessPhotoes.setVisibility(View.VISIBLE);
		}
		if (TextUtil.stringIsNull(result.getAct_id())) {
			businessAction.setVisibility(View.GONE);
		} else {
			afinal.display(actionPhoto,
					ConnectionUtil.localUrl + result.getAct_img());
			actionDisc.setText(result.getAct_content());
			businessAction.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, EnentItemActivity.class);
					intent.putExtra("id", result.getAct_id());
					context.startActivity(intent);
				}
			});
		}
	}

	@Override
	public View getView(int position, View conventView, ViewGroup parent) {
		if (position == 0) {
			conventView = layoutInflater.inflate(R.layout.business_info, null);
			businessInfoDianping = (LinearLayout) conventView
					.findViewById(R.id.business_info_dianping_ll);
			businessPhotoes = (LinearLayout) conventView
					.findViewById(R.id.business_photoes_ll);
			businessTime = (TextView) conventView
					.findViewById(R.id.business_time);
			businessPhone = (TextView) conventView
					.findViewById(R.id.business_phone);
			businessAddress = (TextView) conventView
					.findViewById(R.id.business_address);
			actionDisc = (TextView) conventView.findViewById(R.id.action_disc);
			businessAd = (ImageView) conventView.findViewById(R.id.business_ad);
			businessAction = (LinearLayout) conventView
					.findViewById(R.id.business_action);
			actionPhoto = (ImageView) conventView.findViewById(R.id.action_img);
			fiveStarLayout = (LinearLayout) conventView
					.findViewById(R.id.business_grade);
			fiveStar = new FiveStarsSeekBar(context, EquipmentUtil.dip2px(
					context, 90), EquipmentUtil.dip2px(context, 18));
			// fiveStar = new FiveStarsSeekBar(context, 120, 24);
			fiveStar.setSelectable(false);
			hListView = (HorizontialListView) conventView
					.findViewById(R.id.business_photoes);
			if (businessDetail == null) {
				businessPhotoes.setVisibility(View.GONE);
			} else {
				businessPhotoes.setVisibility(View.VISIBLE);
				setData(businessDetail);
			}
		} else {
			CommentInfoVo temp = commentList.get(position - 1);
			Comm comm;
			if (null == conventView || null == conventView.getTag()) {
				comm = new Comm();
				conventView = layoutInflater.inflate(
						R.layout.business_comment_item, null);
				comm.nick = (TextView) conventView
						.findViewById(R.id.comment_nick);
				comm.content = (TextView) conventView
						.findViewById(R.id.comment_content);
				comm.time = (TextView) conventView
						.findViewById(R.id.comment_time);
				LinearLayout layout = (LinearLayout) conventView
						.findViewById(R.id.five_star);
				FiveStarsSeekBar fiveStars = new FiveStarsSeekBar(context,
						EquipmentUtil.dip2px(context, 90),
						EquipmentUtil.dip2px(context, 18));
				fiveStars.setSelectable(false);
				layout.addView(fiveStars);
				comm.fiveStar = fiveStars;
				conventView.setTag(comm);
			} else {
				comm = (Comm) conventView.getTag();
			}
			if (position == 1) {
				conventView.findViewById(R.id.comment_list).setVisibility(
						View.VISIBLE);
			} else {
				conventView.findViewById(R.id.comment_list).setVisibility(
						View.GONE);
			}
			if (commentList != null) {
				comm.nick.setText(temp.getNickname());
				comm.content.setText(temp.getContent());
				comm.time.setText(temp.getCreate_time());
				if (TextUtil.stringIsNull(temp.getComment_star())) {
					comm.fiveStar.setSelectNums(0);
				} else {
					comm.fiveStar.setSelectNums(Integer.valueOf(temp
							.getComment_star()));
				}
				// 当详情页没有返回评论列表的时候，则手动添加了一条空的第一个item，仅为了显示评论两个字。
				if (MContants.EMPTY_STRING.equals(temp.getNickname())
						&& MContants.EMPTY_STRING.equals(temp.getUser_id())) {
					comm.fiveStar.setVisibility(View.GONE);
				}
				businessInfoDianping.setVisibility(View.GONE);
			} else {
				businessInfoDianping.setVisibility(View.VISIBLE);
			}
			// 评论增加照片显示
			// if(null !=temp.getImglist() && !temp.getImglist().isEmpty() &&
			// 0!=temp.getImglist().size()){
			// comm.hListView.setVisibility(View.VISIBLE);
			// for(int i = temp.getImglist().size(); i<5 ;i++){
			// temp.getImglist().add("");
			// }
			// final ArrayList<String> images = temp.getImglist();
			// comm.hListView.setAdapter(new PhotoAdapter(context,
			// temp.getImglist(), PhotoAdapter.TAG_SHOW));
			// comm.hListView.setOnItemClickListener(new OnItemClickListener() {
			// @Override
			// public void onItemClick(AdapterView<?> parent, View view, int
			// imgPosition, long id) {
			// Intent intent = new Intent(context, PhotoBigActivity.class);
			// intent.putExtra(PhotoBigActivity.PHOTOBIG, new PhotoInfoVo(
			// images.get(imgPosition),""));
			// context.startActivity(intent);
			// }
			// });
			// }else{
			// comm.hListView.setVisibility(View.GONE);
			// }
		}
		return conventView;
	}

	class Comm {
		TextView nick;
		TextView content;
		TextView time;
		FiveStarsSeekBar fiveStar;
		// HorizontialListView hListView;
		PhotoAdapter photoAdpter;
	}

	class HorizontialListViewAdapter extends BaseAdapter {
		private ArrayList<String> images;

		HorizontialListViewAdapter(ArrayList<String> images) {
			this.images = images;
		}

		@Override
		public int getCount() {
			return images.size();
		}

		@Override
		public String getItem(int arg0) {
			return images.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// Button button = new Button(SettleDownDetailsActivity.this);
			LinearLayout layout = new LinearLayout(context);
			layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT));
			ImageView iv = new ImageView(context); // 128
			iv.setPadding(5, 0, 5, 0); // *
			iv.setLayoutParams(lpLvItem);
			layout.addView(iv);
			finalBitmap43.display(iv,
					ConnectionUtil.localUrl + images.get(arg0));
			return layout;
		}
	}
}
